// pages/root/root.js
var AccessToken=""
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin:false,
    isopen:true,
    user:'',
    password:'',
    projectid:'',
    projectname:'',
    project:[],
    divices:[],
    isAdd:false,
    addName:'',
    addleibie:'',
    lianwang:'',
    jianjie:'',
    isupdate:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    setInterval(function(){
      that.getProjects()
    },3000)
  },

  add(e){
    if(this.data.isAdd){
      this.setData({
        isAdd:false
      })
    }else{
      this.setData({
        isAdd:true
      })
    }
  },

  name(e){
    this.setData({
      addName:e.detail.value
    })
  },

  leib1(e){
    if(e.detail.value){
      this.setData({
        addleibie:'监控安防'
      })
    }else{
      this.setData({
        addleibie:''
      })
    }
  },
  leib2(e){
    if(e.detail.value){
      this.setData({
        addleibie:'智能家居'
      })
    }else{
      this.setData({
        addleibie:''
      })
    }
  },
  leib3(e){
    if(e.detail.value){
      this.setData({
        addleibie:'可穿戴设备'
      })
    }else{
      this.setData({
        addleibie:''
      })
    }
  },
  leib4(e){
    if(e.detail.value){
      this.setData({
        addleibie:'智慧农业'
      })
    }else{
      this.setData({
        addleibie:''
      })
    }
  },
  leib5(e){
    if(e.detail.value){
      this.setData({
        addleibie:'工业物联'
      })
    }else{
      this.setData({
        addleibie:''
      })
    }
  },
  leib6(e){
    if(e.detail.value){
      this.setData({
        addleibie:'新能源'
      })
    }else{
      this.setData({
        addleibie:''
      })
    }
  },
  leib7(e){
    if(e.detail.value){
      this.setData({
        addleibie:'楼宇办公'
      })
    }else{
      this.setData({
        addleibie:''
      })
    }
  },
  leib8(e){
    if(e.detail.value){
      this.setData({
        addleibie:'智能玩具'
      })
    }else{
      this.setData({
        addleibie:''
      })
    }
  },
  leib9(e){
    if(e.detail.value){
      this.setData({
        addleibie:'智慧城市'
      })
    }else{
      this.setData({
        addleibie:''
      })
    }
  },

  lianw1(e){
    if(e.detail.value){
      this.setData({
        lianwang:'Wifi'
      })
    }else{
      this.setData({
        lianwang:''
      })
    }
  },
  lianw2(e){
    if(e.detail.value){
      this.setData({
        lianwang:'以太网'
      })
    }else{
      this.setData({
        lianwang:''
      })
    }
  },
  lianw3(e){
    if(e.detail.value){
      this.setData({
        lianwang:'蜂窝网络'
      })
    }else{
      this.setData({
        lianwang:''
      })
    }
  },
  lianw4(e){
    if(e.detail.value){
      this.setData({
        lianwang:'蓝牙'
      })
    }else{
      this.setData({
        lianwang:''
      })
    }
  },
  lianw5(e){
    if(e.detail.value){
      this.setData({
        lianwang:'NB-IoT'
      })
    }else{
      this.setData({
        lianwang:''
      })
    }
  },

  jianjie(e){
    this.setData({
      jianjie:e.detail.value
    })
  },

  tianjia(e){
    var that=this
    if(this.data.addName != '' && this.data.addleibie != '' && this.data.lianwang != ''){
      wx.request({
        url: 'http://api.nlecloud.com/Projects',
        method:"POST",
        data:{
          Industry:this.data.addleibie,
          NetWorkKind:this.data.lianwang,
          Name:this.data.addName,
          Remark:this.data.jianjie,
        },  
        header:{AccessToken},
        success:res=>{
          wx.showToast({
            title: '添加成功',
            icon:'success'
          })
        }
      })
    }else{
      wx.showToast({
        title: '输入为空',
        icon:'error'
      })
    }
  },

  getProjects(e){
    wx.request({
      url: 'http://api.nlecloud.com/Projects',
      method:"GET",
      header:{AccessToken},
      success:res=>{
        this.setData({
          project:res.data.ResultObj
        })
      }
    })
  },

  openxiangmu(e){
    this.setData({
      isopen:false,
      projectid:e.currentTarget.dataset.id,
      projectname:e.currentTarget.dataset.name
    })
    this.getDevices();
  },

  closexiangmu(e){
    this.setData({
      isopen:true,
      divices:[]
    })
  },

  getDevices(e){
    wx.request({
      url: 'http://api.nlecloud.com/Projects/{projectId}/Sensors?projectId='+this.data.projectid,
      header:{AccessToken},
      success:res=>{
        if(res.data.ResultObj.length!=0){
          this.setData({
            divices:res.data.ResultObj
          })
        }else{
          wx.showToast({
            title: '此项目为空',
            icon:'error'
          })
        }
      }
    })
  },

  login(e){
    if(this.data.user != '' && this.data.password != ''){
      wx.request({
        url: 'http://api.nlecloud.com/users/login',
        method:"POST",
        data:{
          Account: this.data.user,
          Password: this.data.password,
        },
        success:res=>{
          if(res.data.ResultObj!=null){
            this.setData({
              isLogin:true,
            }),
            AccessToken=res.data.ResultObj.AccessToken
            this.getProjects();
          }else{
            wx.showModal({
              title: '登录失败',
              content: res.data.Msg,
            })
          }
        }
      })
    }else{
      wx.showToast({
        title: '输入为空',
        icon:'error'
      })
    }
  },

  user(e){
    var that=this
    that.setData({
      user:e.detail.value
    })
  },

  password(e){
    var that=this
    that.setData({
      password:e.detail.value
    })
  },

  isRemem(e){
    var that=this
    var remem=e.detail.value
    that.setData({
      isRemember:e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})